<?php
require_once '../library/class.grabber.php';

class test extends Grabber
{
	public function __construct($argv){
		parent::__construct($argv);
		$this->driver['baseUrl'] = 'http://www.hoepli.it';
		$this->driver['targetUrl'] = 'http://www.hoepli.it';
		//$this->fase1();
		//$this->fase2();
		//$this->fase3();
	}
	
	public function fase1()
	{
		//var_dump('fase1');
		$this->setUserAgent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100401 Ubuntu/9.10 (karmic) Firefox/3.5.9');
		//$this->init();
		$this->http->execute('http://www.cercalibro.it/test/hoepli.html');

		$tidyConfig = array(
			'indent'         					=> true,
			'quote-nbsp'						=> false,
			'output-xhtml'   					=> true,
			'wrap'           					=> 200,
		);

		$html = tidy_repair_string($this->http->result, $tidyConfig, 'latin1');

		$aQuery = array("url" 			=> "//p[@class='classLevel0']/a/@href",
						"Section"		=> "//p[@class='classLevel0']/a/b"

		);
		
		$result = $this->query($html, $aQuery);
		
		if(true){
			$result = array_slice($result, 1, 1);
		}
		
		foreach($result as $key => &$value){
			//var_dump($value);exit;
			$value["url"] = $this->driver['baseUrl'] . $value['url'] . '&pg=' . $i;
		}
		
		/********************************** 
		$this->http->execute('http://www.hoepli.it/libri/agraria_.asp?ty=bk&pc=000033000000000&pg=5000');
		$html = tidy_repair_string($this->http->result, $tidyConfig, 'latin1');

		$aQuery = array("pagine" 			=> "//span[@class='pager_current']",

		);
		$res = array_pop($this->query($html, $aQuery));
		$totPag = $res['pagine'];
		*/
		$totPage = 118;
		for($i=1; $i <= $totPage; $i++){
			$result2[] = "http://www.hoepli.it/libri/agraria_.asp?ty=bk&pc=000033000000000&pg=$i";
		}
		var_dump($result2);
		$this->saveFase1($result2);
		
	}
	
	public function fase2()
	{
		//$a = $this->guessLastPage('http://cercalibro.it/test/guessLastPage.php?pag=', "//body/h1", 'Trovato');
		$a = $this->guessLastPage('http://www.hoepli.it/libri/agraria_.asp?ty=bk&pc=000033000000000&pg=', "/", 'Trovato', 1, 200);
		//var_dump($a);
	}
}
